/**
 * Created by meiguizhinian on 2018/7/23
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIScale9SpriteBox = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();

		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith: function (formName, blockName, sourceControlData) {
		this.init();
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;

		let backLayerColor = uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_CoverLayer_color]);
		if (backLayerColor > 0) {
			let layerBack = new cc.LayerColor(ivy.Tools.getColor4BFromInt32(backLayerColor));
			layerBack.setCascadeColorEnabled(true);
			layerBack.setCascadeOpacityEnabled(true);
			layerBack.setScale(10000);
			this.addChild(layerBack, -1);
		}
		let stt = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_SwallowTouchType_list]);
		if (stt !== ivy.UIScale9SpriteBox.SwallowTouchType.eNone) {
			let listener1 = {};
			if (stt === ivy.UIScale9SpriteBox.SwallowTouchType.eBoxSize) {
				listener1.onTouchBegan = function(touch, event) {
					if (!this.isVisible())
						return false;
					if (this.getOpacity() <= 0)
						return false;
					let touchPoint = this.convertTouchToNodeSpace(touch);
					let touchRect = this.getTouchRect();
					if (cc.rectContainsPoint(touchRect, touchPoint)) {
						if (this._touchEnterFunc) {
							this._touchEnterFunc(this);
						}
						return true;
					}
					return false;
				}.bind(this);

				listener1.onTouchEnded = function(touch, event) {
					let touchPoint = this.convertTouchToNodeSpace(touch);
					let touchRect = this.getTouchRect();
					if (cc.rectContainsPoint(touchRect, touchPoint)) {
						if (this._touchEndFunc) {
							this._touchEndFunc(this);
						}
					}
				}.bind(this);
			} else if (stt === ivy.UIScale9SpriteBox.SwallowTouchType.eAllScreen) {
				let swallowTouchOutZoneAction = uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_SwallowTouchOutZoneClick_action]);
				listener1.onTouchBegan = function(touch, event) {
					if (!this.isVisible())
						return false;
					if (this._touchEnterFunc) {
						this._touchEnterFunc(this);
					}
					return true;
				}.bind(this);
				listener1.onTouchEnded = function(touch, event){
					let touchPoint = this.convertTouchToNodeSpace(touch);
					let touchRect = this.getTouchRect();
					if (this._touchEndFunc) {
						this._touchEndFunc(this);
					}
					if (!cc.rectContainsPoint(touchRect, touchPoint)) {
						if (swallowTouchOutZoneAction >= 0)
							this.runAction(ivy.CocosActionDataManager.getInstance().getGameActionBy(swallowTouchOutZoneAction));
					}
				}.bind(this);
			}
			listener1 = cc.EventListener.create(listener1);

			this.setTouchListener(listener1);
			cc.eventManager.addListener(listener1, this);
			this.setSwallowTouches(true);
		}

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = ivy.UIBase.UIAnchorType.eAnchorHV;
		this.setAnchorType(anchorType);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_OutAction_action]));
		this.setUIElementName(formName, blockName);

		let srcImagePath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_SrcImagePath_string]);
		let fileExist = cc.FileUtils.getInstance().isFileExist(srcImagePath);
		if (fileExist) {
			let leftInset = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_LeftInset_float]);
			let rightInset = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_RightInset_float]);
			let topInset = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_TopInset_float]);
			let bottomInset = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Scale9SpriteBox_BottomInset_float]);
			this._scale9Sprite = new ccui.Scale9Sprite(srcImagePath);
			this._scale9Sprite.setInsetLeft(leftInset);
			this._scale9Sprite.setInsetRight(rightInset);
			this._scale9Sprite.setInsetTop(topInset);
			this._scale9Sprite.setInsetBottom(bottomInset);
			this._scale9Sprite.setContentSize(cc.size(w,h));
			this._scale9Sprite.setCascadeColorEnabled(true);
			this._scale9Sprite.setCascadeOpacityEnabled(true);
			this.addChild(this._scale9Sprite);
		}

		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_scale9Sprite : null,//cocos2d::ui::Scale9Sprite*
});

ivy.UIScale9SpriteBox.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIScale9SpriteBox(formName, blockName, sourceControlData);
};


ivy.UIScale9SpriteBox.SwallowTouchType = {
	eNone       : 0,
	eBoxSize    : 1,
	eAllScreen  : 2,
};